package spider;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author
 * @date 2021年11月05日 19:56
 * @description
 */
public class LagouSearcher {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", LagouSearcher.class.getClassLoader().getResource("chromedriver.exe").getPath());

        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");

        // 创建webdriver
        WebDriver webDriver = new ChromeDriver(options);
        System.out.println(webDriver.getTitle());

        webDriver.get("https://www.lagou.com/zhaopin/Java/?labelWords=label");


        clickOption(webDriver, "工作经验", "在校/应届");
//        Thread.sleep(3000);

        //移动到页面最顶部
        ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(window.pageXOffset, 0)");
        clickOption(webDriver, "学历要求", "本科");
        clickOption(webDriver, "融资阶段", "不限");
        clickOption(webDriver, "公司规模", "不限");
        clickOption(webDriver, "行业领域", "移动互联网");

        extractJobsByPagination(webDriver);

    }

    private static void extractJobsByPagination(WebDriver webDriver) throws InterruptedException {
        List<WebElement> jobElements = webDriver.findElements(By.className("con_list_item"));
        for (WebElement jobElement : jobElements) {
            WebElement moneyElement = jobElement.findElement(By.className("position")).findElement(By.className("money"));
            String company_name = jobElement.findElement(By.className("company_name")).getText();
            System.out.println(company_name + ":" + moneyElement.getText());
        }

        WebElement nextPageBtn = webDriver.findElement(By.className("pager_next"));
        if (!nextPageBtn.getAttribute("class").contains("pager_next_disabled")) {
            nextPageBtn.click();
            webDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
            System.out.println("解析下一页");
            extractJobsByPagination(webDriver);
        }
    }

    private static void clickOption(WebDriver webDriver, String choseTitle, String optionTitle) {
        WebElement chosenElement = webDriver.findElement(By.xpath("//li[@class='multi-chosen']//span[contains(text(),'" + choseTitle + "')]"));
        WebElement optionElement = chosenElement.findElement(By.xpath("../a[contains(text(),'" + optionTitle + "')]"));

        optionElement.click();
//        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    }
}
